/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/20
 * Time:23:27
 */

public class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        slow.next = reverseList(slow.next);
        ListNode start = slow.next;
        while(head != null && start != null){
            if(head.val != start.val){
                return false;
            }
            head = head.next;
            start = start.next;
        }
        return true;
    }
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        if(head.next == null) return head;
        ListNode pre = new ListNode(-1);
        ListNode index = pre;
        ListNode cur = head;
        pre.next = cur;
        while (cur.next != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        cur.next = pre;
        index.next = null;
        head.next = null;
        return cur;
    }
}
